/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;
import entity.Book;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.BookManager;
import manager.DisabledBookManager;
/**
 *
 * @author User
 */
public class BookDB implements IManagerDB<BookManager,DisabledBookManager> 
{
    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;
    
    public BookDB(java.sql.Connection conn)
    {
        this.conn=conn;
    }

    @Override
    public void loadData(BookManager t, boolean value) {
        try {
            cstmt=conn.prepareCall("{call sp_GetBook(?)}");
            cstmt.setBoolean("Status", value);
            rs = cstmt.executeQuery();
            
            while(rs.next())
            {
                int bookID= rs.getInt("BookID");
                String title = rs.getString("Title");
                String authorName = rs.getString("AuthorName");
                int categoryID = rs.getInt("CategoryID");
                int publisherID = rs.getInt("PublisherID");
                String callNumber = rs.getString("CallNumber");
                String isbn = rs.getString("ISBN");
                Float price = rs.getFloat("Price");
                int page = rs.getInt("Page");
                boolean issueStatus = rs.getBoolean("IssueStatus");
                boolean status = rs.getBoolean("Status");
                
                Book book = new Book(bookID, title, authorName, categoryID, publisherID, callNumber, isbn, price, page, issueStatus, status);
                t.add(book);
            }
        } catch (SQLException ex) {
            Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void insert(Object object) {
        Book book = (Book)object;
       
       try {
            cstmt = conn.prepareCall("{call sp_InsertBook(?,?,?,?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1, book.getBookID());
            cstmt.setString(2, book.getTitle());
            cstmt.setString(3, book.getAuthorName());
            cstmt.setInt(4, book.getCategoryID());
            cstmt.setInt(5, book.getPublisherID());
            cstmt.setString(6, book.getCallNumber());
            cstmt.setString(7, book.getIsbn());
            cstmt.setFloat(8, book.getPrice());
            cstmt.setInt(9, book.getPage());
            cstmt.setBoolean(10, book.isIssueStatus());
            cstmt.setBoolean(11, book.isStatus());
            
            
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
            Book book = (Book)object;
        try {
            cstmt = conn.prepareCall("{call sp_UpdateBook(?,?,?,?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1, book.getBookID());
            cstmt.setString(2, book.getTitle());
            cstmt.setString(3, book.getAuthorName());
            cstmt.setInt(4, book.getCategoryID());
            cstmt.setInt(5, book.getPublisherID());
            cstmt.setString(6, book.getCallNumber());
            cstmt.setString(7, book.getIsbn());
            cstmt.setFloat(8, book.getPrice());
            cstmt.setInt(9, book.getPage());
            cstmt.setBoolean(10, book.isIssueStatus());
            cstmt.setBoolean(11, book.isStatus());
            
            
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
}
